﻿using System;
using System.Linq;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using Ah.Testudin.Engine.Events;
using Ah.Testudin.Engine.Graphics.Scenes;
using Ah.Testudin.Engine.Actors.Components;
using Ah.Testudin.Engine.UI.Elements;

namespace Ah.Testudin.Engine.Input
{
    public class TortoiseController
    {
        #region Private Member Variables

        private const float THRUST_NEWTONS = .5f;

        private readonly ISceneNode _sceneNode;

        private readonly IEventManager _eventManager;

        private bool _enterPushed;

        #endregion

        #region Constructors

        public TortoiseController(
            ISceneNode sceneNode,
            IEventManager eventManager)
        {
            if (sceneNode == null)
            {
                throw new ArgumentNullException("sceneNode");
            }

            if (eventManager == null)
            {
                throw new ArgumentNullException("eventManager");
            }

            _sceneNode = sceneNode;

            _eventManager = eventManager;

            _enterPushed = false;
        }

        #endregion

        #region Public Methods

        public void Update()
        {
            KeyboardState kbState = Keyboard.GetState();

            if (kbState.IsKeyDown(Keys.Enter) &&
                (_enterPushed == false))
            {
                _enterPushed = true;
            }

            if (kbState.IsKeyUp(Keys.Enter) &&
                (_enterPushed))
            {
                _enterPushed = false;

                _eventManager.QueueEvent(
                    new MissileFiredEvent(
                        _sceneNode.Properties.ActorId));
            }

            if (kbState.IsKeyDown(Keys.Space))
            {
                _eventManager.QueueEvent(
                    new ActorAcceleratingEvent(
                        _sceneNode.Properties.ActorId,
                        THRUST_NEWTONS));
            }
            else
            {
                _eventManager.QueueEvent(
                    new ActorDeceleratingEvent(
                        _sceneNode.Properties.ActorId));
            }

            if (kbState.IsKeyDown(Keys.W))
            {
                _eventManager.QueueEvent(
                    new ActorMovingEvent(
                        _sceneNode.Properties.ActorId,
                        MovementDirection.Forward));
            }

            if (kbState.IsKeyDown(Keys.A))
            {
                if (kbState.IsKeyDown(Keys.RightShift))
                {
                    _eventManager.QueueEvent(
                        new ActorRotatingEvent(
                            _sceneNode.Properties.ActorId,
                            RotationDirection.CounterClockwise));
                }
                else
                {
                    _eventManager.QueueEvent(
                        new ActorMovingEvent(
                            _sceneNode.Properties.ActorId,
                            MovementDirection.Left));
                }
            }

            if (kbState.IsKeyDown(Keys.S))
            {
                _eventManager.QueueEvent(
                    new ActorMovingEvent(
                        _sceneNode.Properties.ActorId,
                        MovementDirection.Backward));
            }

            if (kbState.IsKeyDown(Keys.D))
            {
                if (kbState.IsKeyDown(Keys.RightShift))
                {
                    _eventManager.QueueEvent(
                        new ActorRotatingEvent(
                            _sceneNode.Properties.ActorId,
                            RotationDirection.Clockwise));
                }
                else
                {
                    _eventManager.QueueEvent(
                        new ActorMovingEvent(
                            _sceneNode.Properties.ActorId,
                            MovementDirection.Right));
                }
            }

        }

        #endregion
    }
}
